<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Activation Code Query</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
        }
        .input-group {
            margin-bottom: 20px;
        }
        input[type="text"] {
            padding: 8px;
            width: 300px;
            margin-right: 10px;
        }
        button {
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>授权码激活/查询</h1>

    <div class="input-group">
        <input type="text" id="licenseInput_active" placeholder="Enter license key">
        <button onclick="activeAuthCode()">激活授权码</button>
    </div>

    <div class="input-group">
        <input type="text" id="licenseInput_query" placeholder="Enter license key">
        <button onclick="queryAuthCode()">查询授权码</button>
    </div>

    <div id="resultSection" style="display: none;">
        <h3>查询结果：</h3>
        <table>
            <thead>
            <tr>
                <th>授权码</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td id="authCodeResult"></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
    function activeAuthCode() {
        const licenseKey = $('#licenseInput_active').val().trim();

        if (!licenseKey) {
            alert('Please enter a license key');
            return;
        }

        // Call backend API
        $.ajax({
            url: '/auth/code',
            type: 'POST', // or 'GET' depending on your backend
            data: {
                type: 'active',
                license: licenseKey
            },
            success: function(response) {
                $('#authCodeResult').text(response.authCode || response);
                $('#resultSection').show();
            },
            error: function(xhr, status, error) {
                alert('Error active code: ' + (xhr.responseText || error));
                console.error(error);
            }
        });
    }

    // Optional: Allow pressing Enter key to submit
    $('#licenseInput_active').keypress(function(e) {
        if (e.which === 13) {
            activeAuthCode();
        }
    });
    
    function queryAuthCode() {
        const licenseKey = $('#licenseInput_query').val().trim();

        if (!licenseKey) {
            alert('Please enter a license key');
            return;
        }

        // Call backend API
        $.ajax({
            url: '/auth/code',
            type: 'POST', // or 'GET' depending on your backend
            data: {
                type: 'query',
                license: licenseKey
            },
            success: function(response) {
                $('#authCodeResult').text(response.authCode || response);
                $('#resultSection').show();
            },
            error: function(xhr, status, error) {
                alert('Error querying active code: ' + (xhr.responseText || error));
                console.error(error);
            }
        });
    }

    // Optional: Allow pressing Enter key to submit
    $('#licenseInput_query').keypress(function(e) {
        if (e.which === 13) {
            queryAuthCode();
        }
    });
</script>
</body>
</html>